package com.xja.webchat.controller;

import com.github.pagehelper.Page;
import com.xja.webchat.entity.Chatuser;
import com.xja.webchat.entity.Reply;
import com.xja.webchat.entity.Space;
import com.xja.webchat.service.ChatUserService;
import com.xja.webchat.service.ReplyService;
import com.xja.webchat.service.SpaceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class FriendController {
    private Logger logger = LoggerFactory.getLogger(FriendController.class);
    @Autowired
    private ReplyService replyService;
    @Autowired
    private SpaceService spaceService;
    @Autowired
    private ChatUserService chatUserService;

    @GetMapping("chazhaohaoyou")
    public Map<String,Object> chazhaohaoyou(Integer npage, String keyword){
        Map<String,Object> resuMap = new HashMap<>();
        try{
            logger.info("调用了查找好友的方法");
            System.out.println("keyword"+keyword);
            List<Chatuser> chatuserList = chatUserService.chazhaohaoyou(keyword);
            resuMap.put("status","true");
            resuMap.put("chatuserList",chatuserList);
            return resuMap;

        }catch (Exception ex){
            logger.error("有问题",ex);
        }
        resuMap.put("status","false");
        return resuMap;
    }

    @GetMapping("friend")
    public Map<String,Object> haoYouDongTai(Integer npage, HttpSession session){
        Map<String,Object> resuMap = new HashMap<>();
        try{
            logger.info("调用了查询Space的方法");

            if(npage==null ||"".equals(npage))
                npage=1;

            Chatuser loginUser = (Chatuser) session.getAttribute("loginUser");
            Page<Space> spacePage = spaceService.selectHaoYouDongTai(npage, loginUser.getId());
            List<Reply> replyList = new ArrayList<>();
            for (Space spage:spacePage.getResult()){
                replyList = replyService.selectBySid(spage.getId());
            }

            resuMap.put("status","true");
            resuMap.put("replyList",replyList);
            resuMap.put("spaceList",spacePage.getResult());
            resuMap.put("npage",npage);
            return resuMap;

        }catch (Exception ex){
            logger.error("有问题",ex);
        }
        resuMap.put("status","false");
        return resuMap;
    }
}
